﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimingSetter
{
    public partial class Form1 : Form
    {
        // 時間計測用
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

        // タイマの役割
        enum TimerRole
        {
            CountDown3,
            TimerNormal,
            NoRole,
            INVALID,
        }
        TimerRole tr = TimerRole.NoRole;

        // Setされた時間格納
        List<long> setTime = new List<long>();


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btnSet.Enabled = false;
            btnOutput.Enabled = false;
            tbTimeSet.Text = "";
            timer1.Enabled = false;
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            long thetime = sw.ElapsedMilliseconds;
            setTime.Add(thetime);
            tbTimeSet.AppendText(((double)thetime/1000).ToString()+Environment.NewLine);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            tbFname.Enabled = false;
            lblTime.Text = "CD3";
            tr = TimerRole.CountDown3;
            sw.Start();
/*
            Console.Beep();
            System.Threading.Thread.Sleep(1000);
            Console.Beep();
            System.Threading.Thread.Sleep(1000);
            Console.Beep();
            System.Threading.Thread.Sleep(1000);
*/
            timer1.Interval = 1000;
            timer1.Enabled = true;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (tr)
            {
/*                case TimerRole.CountDown3:
                    lblTime.Text = "CD: " + ((double)(3000 - sw.ElapsedMilliseconds) / 1000).ToString();
                    if (sw.ElapsedMilliseconds > 3000)
                    {  // TimerNormalに移行
                        tr = TimerRole.TimerNormal;
                        btnSet.Enabled = true;
                        btnOutput.Enabled = true;
                        
                        sw.Reset();
                        sw.Start();
                    }
                    break;
*/
                case TimerRole.CountDown3:
                    if (sw.ElapsedMilliseconds > 3000)
                    {
                        Console.Beep(3000, 200);
                        // TimerNormalに移行
                        tr = TimerRole.TimerNormal;
                        btnSet.Enabled = true;
                        btnOutput.Enabled = true;
                        timer1.Interval = 10;

                        sw.Reset();
                        sw.Start();
                    }
                    else
                    {
                        Console.Beep();
                    }
                    break;
                    
                case TimerRole.TimerNormal:
                    lblTime.Text = ((double)sw.ElapsedMilliseconds / 1000).ToString();
                    break;
            }
        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            tr = TimerRole.NoRole;
            sw.Stop();
            btnSet.Enabled = false;
            btnOutput.Enabled = false;

            // ファイル出力
            String path = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\" + tbFname.Text;
            while (System.IO.File.Exists(path))
            {
                path += ".txt";
            }
            System.IO.StreamWriter swr = new System.IO.StreamWriter(
                path,
                false,
                System.Text.Encoding.GetEncoding("shift_jis")
            );
            foreach (long l in setTime)
            {
                swr.WriteLine(l);
            }
            swr.Close();
            lblResult.Text = "Written at " + path;
        }
    }
}
